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>
21 #define DBG(x...) do {} while (0)
23 #define DBG(x...) printf(x)
24 static const char *reqname(unsigned r)
27 case USB_REQ_GET_STATUS: return "GET_STATUS";
28 case USB_REQ_CLEAR_FEATURE: return "CLEAR_FEATURE";
29 case USB_REQ_SET_FEATURE: return "SET_FEATURE";
30 case USB_REQ_SET_ADDRESS: return "SET_ADDRESS";
31 case USB_REQ_GET_DESCRIPTOR: return "GET_DESCRIPTOR";
32 case USB_REQ_SET_DESCRIPTOR: return "SET_DESCRIPTOR";
33 case USB_REQ_GET_CONFIGURATION: return "GET_CONFIGURATION";
34 case USB_REQ_SET_CONFIGURATION: return "SET_CONFIGURATION";
35 case USB_REQ_GET_INTERFACE: return "GET_INTERFACE";
36 case USB_REQ_SET_INTERFACE: return "SET_INTERFACE";
37 default: return "*UNKNOWN*";
42 #define PAGE_SIZE 4096
44 static struct usb_endpoint_descriptor ep0_out_desc = {
45 .bLength = sizeof(struct usb_endpoint_descriptor),
46 .bDescriptorType = USB_DT_ENDPOINT,
47 .bEndpointAddress = 0,
48 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
51 static struct usb_endpoint_descriptor ep0_in_desc = {
52 .bLength = sizeof(struct usb_endpoint_descriptor),
53 .bDescriptorType = USB_DT_ENDPOINT,
54 .bEndpointAddress = USB_DIR_IN,
55 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
58 struct ept_queue_head *epts;
59 struct ept_queue_item *items[2 * NUM_ENDPOINTS];
60 static int mv_pullup(struct usb_gadget *gadget, int is_on);
61 static int mv_ep_enable(struct usb_ep *ep,
62 const struct usb_endpoint_descriptor *desc);
63 static int mv_ep_disable(struct usb_ep *ep);
64 static int mv_ep_queue(struct usb_ep *ep,
65 struct usb_request *req, gfp_t gfp_flags);
66 static struct usb_request *
67 mv_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags);
68 static void mv_ep_free_request(struct usb_ep *ep, struct usb_request *_req);
70 static struct usb_gadget_ops mv_udc_ops = {
74 static struct usb_ep_ops mv_ep_ops = {
75 .enable = mv_ep_enable,
76 .disable = mv_ep_disable,
78 .alloc_request = mv_ep_alloc_request,
79 .free_request = mv_ep_free_request,
82 static struct mv_ep ep[2 * NUM_ENDPOINTS];
83 static struct mv_drv controller = {
90 static struct usb_request *
91 mv_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags)
93 struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
97 static void mv_ep_free_request(struct usb_ep *ep, struct usb_request *_req)
102 static void ep_enable(int num, int in)
104 struct ept_queue_head *head;
105 struct mv_udc *udc = controller.udc;
107 head = epts + 2*num + in;
109 n = readl(&udc->epctrl[num]);
111 n |= (CTRL_TXE | CTRL_TXR | CTRL_TXT_BULK);
113 n |= (CTRL_RXE | CTRL_RXR | CTRL_RXT_BULK);
116 head->config = CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE) | CONFIG_ZLT;
117 writel(n, &udc->epctrl[num]);
120 static int mv_ep_enable(struct usb_ep *ep,
121 const struct usb_endpoint_descriptor *desc)
123 struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
125 num = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
126 in = (desc->bEndpointAddress & USB_DIR_IN) != 0;
132 static int mv_ep_disable(struct usb_ep *ep)
137 static int mv_ep_queue(struct usb_ep *ep,
138 struct usb_request *req, gfp_t gfp_flags)
140 struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
141 struct mv_udc *udc = controller.udc;
142 struct ept_queue_item *item;
143 struct ept_queue_head *head;
145 int bit, num, len, in;
146 num = mv_ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
147 in = (mv_ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
148 item = items[2 * num + in];
149 head = epts + 2 * num + in;
150 phys = (unsigned)req->buf;
153 item->next = TERMINATE;
154 item->info = INFO_BYTES(len) | INFO_IOC | INFO_ACTIVE;
156 item->page1 = (phys & 0xfffff000) + 0x1000;
158 head->next = (unsigned) item;
161 DBG("ept%d %s queue len %x, buffer %x\n",
162 num, in ? "in" : "out", len, phys);
169 flush_cache(phys, len);
170 flush_cache((unsigned long)item, sizeof(struct ept_queue_item));
171 writel(bit, &udc->epprime);
176 static void handle_ep_complete(struct mv_ep *ep)
178 struct ept_queue_item *item;
180 num = ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
181 in = (ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
183 ep->desc = &ep0_out_desc;
184 item = items[2 * num + in];
186 if (item->info & 0xff)
187 printf("EP%d/%s FAIL nfo=%x pg0=%x\n",
188 num, in ? "in" : "out", item->info, item->page0);
190 len = (item->info >> 16) & 0x7fff;
191 ep->req.length -= len;
192 DBG("ept%d %s complete %x\n",
193 num, in ? "in" : "out", len);
194 ep->req.complete(&ep->ep, &ep->req);
197 usb_ep_queue(&ep->ep, &ep->req, 0);
198 ep->desc = &ep0_in_desc;
202 #define SETUP(type, request) (((type) << 8) | (request))
204 static void handle_setup(void)
206 struct usb_request *req = &ep[0].req;
207 struct mv_udc *udc = controller.udc;
208 struct ept_queue_head *head;
209 struct usb_ctrlrequest r;
211 int num, in, _num, _in, i;
215 flush_cache((unsigned long)head, sizeof(struct ept_queue_head));
216 memcpy(&r, head->setup_data, sizeof(struct usb_ctrlrequest));
217 writel(EPT_RX(0), &udc->epstat);
218 DBG("handle setup %s, %x, %x index %x value %x\n", reqname(r.bRequest),
219 r.bRequestType, r.bRequest, r.wIndex, r.wValue);
221 switch (SETUP(r.bRequestType, r.bRequest)) {
222 case SETUP(USB_RECIP_ENDPOINT, USB_REQ_CLEAR_FEATURE):
223 _num = r.wIndex & 15;
224 _in = !!(r.wIndex & 0x80);
226 if ((r.wValue == 0) && (r.wLength == 0)) {
228 for (i = 0; i < NUM_ENDPOINTS; i++) {
231 num = ep[i].desc->bEndpointAddress
232 & USB_ENDPOINT_NUMBER_MASK;
233 in = (ep[i].desc->bEndpointAddress
235 if ((num == _num) && (in == _in)) {
237 usb_ep_queue(controller.gadget.ep0,
245 case SETUP(USB_RECIP_DEVICE, USB_REQ_SET_ADDRESS):
247 * write address delayed (will take effect
248 * after the next IN txn)
250 writel((r.wValue << 25) | (1 << 24), &udc->devaddr);
252 usb_ep_queue(controller.gadget.ep0, req, 0);
255 case SETUP(USB_DIR_IN | USB_RECIP_DEVICE, USB_REQ_GET_STATUS):
257 buf = (char *)req->buf;
258 buf[0] = 1 << USB_DEVICE_SELF_POWERED;
260 usb_ep_queue(controller.gadget.ep0, req, 0);
263 /* pass request up to the gadget driver */
264 if (controller.driver)
265 status = controller.driver->setup(&controller.gadget, &r);
271 DBG("STALL reqname %s type %x value %x, index %x\n",
272 reqname(r.bRequest), r.bRequestType, r.wValue, r.wIndex);
273 writel((1<<16) | (1 << 0), &udc->epctrl[0]);
276 static void stop_activity(void)
279 struct ept_queue_head *head;
280 struct mv_udc *udc = controller.udc;
281 writel(readl(&udc->epcomp), &udc->epcomp);
282 writel(readl(&udc->epstat), &udc->epstat);
283 writel(0xffffffff, &udc->epflush);
285 /* error out any pending reqs */
286 for (i = 0; i < NUM_ENDPOINTS; i++) {
288 writel(0, &udc->epctrl[i]);
290 num = ep[i].desc->bEndpointAddress
291 & USB_ENDPOINT_NUMBER_MASK;
292 in = (ep[i].desc->bEndpointAddress & USB_DIR_IN) != 0;
293 head = epts + (num * 2) + (in);
294 head->info = INFO_ACTIVE;
301 struct mv_udc *udc = controller.udc;
302 unsigned n = readl(&udc->usbsts);
303 writel(n, &udc->usbsts);
306 n &= (STS_SLI | STS_URI | STS_PCI | STS_UI | STS_UEI);
311 DBG("-- reset --\n");
315 DBG("-- suspend --\n");
318 DBG("-- portchange --\n");
319 bit = (readl(&udc->portsc) >> 26) & 3;
321 controller.gadget.speed = USB_SPEED_HIGH;
322 for (i = 1; i < NUM_ENDPOINTS && n; i++)
324 ep[i].ep.maxpacket = 512;
326 controller.gadget.speed = USB_SPEED_FULL;
331 printf("<UEI %x>\n", readl(&udc->epcomp));
333 if ((n & STS_UI) || (n & STS_UEI)) {
334 n = readl(&udc->epstat);
338 n = readl(&udc->epcomp);
340 writel(n, &udc->epcomp);
342 for (i = 0; i < NUM_ENDPOINTS && n; i++) {
344 num = ep[i].desc->bEndpointAddress
345 & USB_ENDPOINT_NUMBER_MASK;
346 in = (ep[i].desc->bEndpointAddress
348 bit = (in) ? EPT_TX(num) : EPT_RX(num);
350 handle_ep_complete(&ep[i]);
356 int usb_gadget_handle_interrupts(void)
359 struct mv_udc *udc = controller.udc;
361 value = readl(&udc->usbsts);
368 static int mv_pullup(struct usb_gadget *gadget, int is_on)
370 struct mv_udc *udc = controller.udc;
373 writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RST, &udc->usbcmd);
376 writel((unsigned) epts, &udc->epinitaddr);
378 /* select DEVICE mode */
379 writel(USBMODE_DEVICE, &udc->usbmode);
381 writel(0xffffffff, &udc->epflush);
383 /* Turn on the USB connection by enabling the pullup resistor */
384 writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RUN, &udc->usbcmd);
387 writel(USBCMD_FS2, &udc->usbcmd);
389 if (controller.driver)
390 controller.driver->disconnect(gadget);
396 void udc_disconnect(void)
398 struct mv_udc *udc = controller.udc;
401 writel(USBCMD_FS2, &udc->usbcmd);
403 if (controller.driver)
404 controller.driver->disconnect(&controller.gadget);
407 static int mvudc_probe(void)
409 struct ept_queue_head *head;
412 controller.gadget.ops = &mv_udc_ops;
413 controller.udc = (struct mv_udc *)CONFIG_USB_REG_BASE;
414 epts = memalign(PAGE_SIZE, QH_MAXNUM * sizeof(struct ept_queue_head));
415 memset(epts, 0, QH_MAXNUM * sizeof(struct ept_queue_head));
416 for (i = 0; i < 2 * NUM_ENDPOINTS; i++) {
418 * For item0 and item1, they are served as ep0
423 head->config = CONFIG_MAX_PKT(EP0_MAX_PACKET_SIZE)
424 | CONFIG_ZLT | CONFIG_IOS;
426 head->config = CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE)
428 head->next = TERMINATE;
431 items[i] = memalign(PAGE_SIZE, sizeof(struct ept_queue_item));
434 INIT_LIST_HEAD(&controller.gadget.ep_list);
435 ep[0].ep.maxpacket = 64;
436 ep[0].ep.name = "ep0";
437 ep[0].desc = &ep0_in_desc;
438 INIT_LIST_HEAD(&controller.gadget.ep0->ep_list);
439 for (i = 0; i < 2 * NUM_ENDPOINTS; i++) {
441 ep[i].ep.maxpacket = 512;
442 ep[i].ep.name = "ep-";
443 list_add_tail(&ep[i].ep.ep_list,
444 &controller.gadget.ep_list);
447 ep[i].ep.ops = &mv_ep_ops;
452 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
454 struct mv_udc *udc = controller.udc;
458 || driver->speed < USB_SPEED_FULL
461 DBG("bad parameter.\n");
465 if (!mvudc_probe()) {
467 /* select ULPI phy */
468 writel(PTS(PTS_ENABLE) | PFSC, &udc->portsc);
470 retval = driver->bind(&controller.gadget);
472 DBG("driver->bind() returned %d\n", retval);
475 controller.driver = driver;
480 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)