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 struct ept_queue_head *epts;
63 struct ept_queue_item *items[2 * NUM_ENDPOINTS];
64 static int mv_pullup(struct usb_gadget *gadget, int is_on);
65 static int mv_ep_enable(struct usb_ep *ep,
66 const struct usb_endpoint_descriptor *desc);
67 static int mv_ep_disable(struct usb_ep *ep);
68 static int mv_ep_queue(struct usb_ep *ep,
69 struct usb_request *req, gfp_t gfp_flags);
70 static struct usb_request *
71 mv_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags);
72 static void mv_ep_free_request(struct usb_ep *ep, struct usb_request *_req);
74 static struct usb_gadget_ops mv_udc_ops = {
78 static struct usb_ep_ops mv_ep_ops = {
79 .enable = mv_ep_enable,
80 .disable = mv_ep_disable,
82 .alloc_request = mv_ep_alloc_request,
83 .free_request = mv_ep_free_request,
86 static struct mv_ep ep[2 * NUM_ENDPOINTS];
87 static struct mv_drv controller = {
94 static struct usb_request *
95 mv_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags)
97 struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
101 static void mv_ep_free_request(struct usb_ep *ep, struct usb_request *_req)
106 static void ep_enable(int num, int in)
108 struct ept_queue_head *head;
109 struct mv_udc *udc = controller.udc;
111 head = epts + 2*num + in;
113 n = readl(&udc->epctrl[num]);
115 n |= (CTRL_TXE | CTRL_TXR | CTRL_TXT_BULK);
117 n |= (CTRL_RXE | CTRL_RXR | CTRL_RXT_BULK);
120 head->config = CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE) | CONFIG_ZLT;
121 writel(n, &udc->epctrl[num]);
124 static int mv_ep_enable(struct usb_ep *ep,
125 const struct usb_endpoint_descriptor *desc)
127 struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
129 num = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
130 in = (desc->bEndpointAddress & USB_DIR_IN) != 0;
136 static int mv_ep_disable(struct usb_ep *ep)
141 static int mv_ep_queue(struct usb_ep *ep,
142 struct usb_request *req, gfp_t gfp_flags)
144 struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
145 struct mv_udc *udc = controller.udc;
146 struct ept_queue_item *item;
147 struct ept_queue_head *head;
149 int bit, num, len, in;
150 num = mv_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
151 in = (mv_ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
152 item = items[2 * num + in];
153 head = epts + 2 * num + in;
154 phys = (unsigned)req->buf;
157 item->next = TERMINATE;
158 item->info = INFO_BYTES(len) | INFO_IOC | INFO_ACTIVE;
160 item->page1 = (phys & 0xfffff000) + 0x1000;
162 head->next = (unsigned) item;
165 DBG("ept%d %s queue len %x, buffer %x\n",
166 num, in ? "in" : "out", len, phys);
173 flush_cache(phys, len);
174 flush_cache((unsigned long)item, sizeof(struct ept_queue_item));
175 writel(bit, &udc->epprime);
180 static void handle_ep_complete(struct mv_ep *ep)
182 struct ept_queue_item *item;
184 num = ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
185 in = (ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
187 ep->desc = &ep0_out_desc;
188 item = items[2 * num + in];
190 if (item->info & 0xff)
191 printf("EP%d/%s FAIL nfo=%x pg0=%x\n",
192 num, in ? "in" : "out", item->info, item->page0);
194 len = (item->info >> 16) & 0x7fff;
195 ep->req.length -= len;
196 DBG("ept%d %s complete %x\n",
197 num, in ? "in" : "out", len);
198 ep->req.complete(&ep->ep, &ep->req);
201 usb_ep_queue(&ep->ep, &ep->req, 0);
202 ep->desc = &ep0_in_desc;
206 #define SETUP(type, request) (((type) << 8) | (request))
208 static void handle_setup(void)
210 struct usb_request *req = &ep[0].req;
211 struct mv_udc *udc = controller.udc;
212 struct ept_queue_head *head;
213 struct usb_ctrlrequest r;
215 int num, in, _num, _in, i;
219 flush_cache((unsigned long)head, sizeof(struct ept_queue_head));
220 memcpy(&r, head->setup_data, sizeof(struct usb_ctrlrequest));
221 writel(EPT_RX(0), &udc->epstat);
222 DBG("handle setup %s, %x, %x index %x value %x\n", reqname(r.bRequest),
223 r.bRequestType, r.bRequest, r.wIndex, r.wValue);
225 switch (SETUP(r.bRequestType, r.bRequest)) {
226 case SETUP(USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE):
227 _num = r.wIndex & 15;
228 _in = !!(r.wIndex & 0x80);
230 if ((r.wValue == 0) && (r.wLength == 0)) {
232 for (i = 0; i < NUM_ENDPOINTS; i++) {
235 num = ep[i].desc->bEndpointAddress
236 & USB_ENDPOINT_NUMBER_MASK;
237 in = (ep[i].desc->bEndpointAddress
239 if ((num == _num) && (in == _in)) {
241 usb_ep_queue(controller.gadget.ep0,
249 case SETUP(USB_RECIP_DEVICE, USB_REQ_SET_ADDRESS):
251 * write address delayed (will take effect
252 * after the next IN txn)
254 writel((r.wValue << 25) | (1 << 24), &udc->devaddr);
256 usb_ep_queue(controller.gadget.ep0, req, 0);
259 case SETUP(USB_DIR_IN | USB_RECIP_DEVICE, USB_REQ_GET_STATUS):
261 buf = (char *)req->buf;
262 buf[0] = 1 << USB_DEVICE_SELF_POWERED;
264 usb_ep_queue(controller.gadget.ep0, req, 0);
267 /* pass request up to the gadget driver */
268 if (controller.driver)
269 status = controller.driver->setup(&controller.gadget, &r);
275 DBG("STALL reqname %s type %x value %x, index %x\n",
276 reqname(r.bRequest), r.bRequestType, r.wValue, r.wIndex);
277 writel((1<<16) | (1 << 0), &udc->epctrl[0]);
280 static void stop_activity(void)
283 struct ept_queue_head *head;
284 struct mv_udc *udc = controller.udc;
285 writel(readl(&udc->epcomp), &udc->epcomp);
286 writel(readl(&udc->epstat), &udc->epstat);
287 writel(0xffffffff, &udc->epflush);
289 /* error out any pending reqs */
290 for (i = 0; i < NUM_ENDPOINTS; i++) {
292 writel(0, &udc->epctrl[i]);
294 num = ep[i].desc->bEndpointAddress
295 & USB_ENDPOINT_NUMBER_MASK;
296 in = (ep[i].desc->bEndpointAddress & USB_DIR_IN) != 0;
297 head = epts + (num * 2) + (in);
298 head->info = INFO_ACTIVE;
305 struct mv_udc *udc = controller.udc;
306 unsigned n = readl(&udc->usbsts);
307 writel(n, &udc->usbsts);
310 n &= (STS_SLI | STS_URI | STS_PCI | STS_UI | STS_UEI);
315 DBG("-- reset --\n");
319 DBG("-- suspend --\n");
322 DBG("-- portchange --\n");
323 bit = (readl(&udc->portsc) >> 26) & 3;
325 controller.gadget.speed = USB_SPEED_HIGH;
326 for (i = 1; i < NUM_ENDPOINTS && n; i++)
328 ep[i].ep.maxpacket = 512;
330 controller.gadget.speed = USB_SPEED_FULL;
335 printf("<UEI %x>\n", readl(&udc->epcomp));
337 if ((n & STS_UI) || (n & STS_UEI)) {
338 n = readl(&udc->epstat);
342 n = readl(&udc->epcomp);
344 writel(n, &udc->epcomp);
346 for (i = 0; i < NUM_ENDPOINTS && n; i++) {
348 num = ep[i].desc->bEndpointAddress
349 & USB_ENDPOINT_NUMBER_MASK;
350 in = (ep[i].desc->bEndpointAddress
352 bit = (in) ? EPT_TX(num) : EPT_RX(num);
354 handle_ep_complete(&ep[i]);
360 int usb_gadget_handle_interrupts(void)
363 struct mv_udc *udc = controller.udc;
365 value = readl(&udc->usbsts);
372 static int mv_pullup(struct usb_gadget *gadget, int is_on)
374 struct mv_udc *udc = controller.udc;
377 writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RST, &udc->usbcmd);
380 writel((unsigned) epts, &udc->epinitaddr);
382 /* select DEVICE mode */
383 writel(USBMODE_DEVICE, &udc->usbmode);
385 writel(0xffffffff, &udc->epflush);
387 /* Turn on the USB connection by enabling the pullup resistor */
388 writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RUN, &udc->usbcmd);
391 writel(USBCMD_FS2, &udc->usbcmd);
393 if (controller.driver)
394 controller.driver->disconnect(gadget);
400 void udc_disconnect(void)
402 struct mv_udc *udc = controller.udc;
405 writel(USBCMD_FS2, &udc->usbcmd);
407 if (controller.driver)
408 controller.driver->disconnect(&controller.gadget);
411 static int mvudc_probe(void)
413 struct ept_queue_head *head;
416 controller.gadget.ops = &mv_udc_ops;
417 controller.udc = (struct mv_udc *)CONFIG_USB_REG_BASE;
418 epts = memalign(PAGE_SIZE, QH_MAXNUM * sizeof(struct ept_queue_head));
419 memset(epts, 0, QH_MAXNUM * sizeof(struct ept_queue_head));
420 for (i = 0; i < 2 * NUM_ENDPOINTS; i++) {
422 * For item0 and item1, they are served as ep0
427 head->config = CONFIG_MAX_PKT(EP0_MAX_PACKET_SIZE)
428 | CONFIG_ZLT | CONFIG_IOS;
430 head->config = CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE)
432 head->next = TERMINATE;
435 items[i] = memalign(PAGE_SIZE, sizeof(struct ept_queue_item));
438 INIT_LIST_HEAD(&controller.gadget.ep_list);
439 ep[0].ep.maxpacket = 64;
440 ep[0].ep.name = "ep0";
441 ep[0].desc = &ep0_in_desc;
442 INIT_LIST_HEAD(&controller.gadget.ep0->ep_list);
443 for (i = 0; i < 2 * NUM_ENDPOINTS; i++) {
445 ep[i].ep.maxpacket = 512;
446 ep[i].ep.name = "ep-";
447 list_add_tail(&ep[i].ep.ep_list,
448 &controller.gadget.ep_list);
451 ep[i].ep.ops = &mv_ep_ops;
456 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
458 struct mv_udc *udc = controller.udc;
463 || driver->speed < USB_SPEED_FULL
466 DBG("bad parameter.\n");
470 if (!mvudc_probe()) {
471 usb_lowlevel_init(0, &ctrl);
472 /* select ULPI phy */
473 writel(PTS(PTS_ENABLE) | PFSC, &udc->portsc);
475 retval = driver->bind(&controller.gadget);
477 DBG("driver->bind() returned %d\n", retval);
480 controller.driver = driver;
485 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)