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 static 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,
53 static 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,
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 /* Init values for USB endpoints. */
83 static const struct usb_ep mv_ep_init[2] = {
96 static struct mv_drv controller = {
103 static struct usb_request *
104 mv_ep_alloc_request(struct usb_ep *ep, unsigned int gfp_flags)
106 struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
110 static void mv_ep_free_request(struct usb_ep *ep, struct usb_request *_req)
115 static void ep_enable(int num, int in)
117 struct ept_queue_head *head;
118 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
120 head = controller.epts + 2*num + in;
122 n = readl(&udc->epctrl[num]);
124 n |= (CTRL_TXE | CTRL_TXR | CTRL_TXT_BULK);
126 n |= (CTRL_RXE | CTRL_RXR | CTRL_RXT_BULK);
129 head->config = CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE) | CONFIG_ZLT;
130 writel(n, &udc->epctrl[num]);
133 static int mv_ep_enable(struct usb_ep *ep,
134 const struct usb_endpoint_descriptor *desc)
136 struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
138 num = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
139 in = (desc->bEndpointAddress & USB_DIR_IN) != 0;
145 static int mv_ep_disable(struct usb_ep *ep)
150 static int mv_ep_queue(struct usb_ep *ep,
151 struct usb_request *req, gfp_t gfp_flags)
153 struct mv_ep *mv_ep = container_of(ep, struct mv_ep, ep);
154 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
155 struct ept_queue_item *item;
156 struct ept_queue_head *head;
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;
161 item = controller.items[2 * num + in];
162 head = controller.epts + 2 * num + in;
163 phys = (unsigned)req->buf;
166 item->next = TERMINATE;
167 item->info = INFO_BYTES(len) | INFO_IOC | INFO_ACTIVE;
169 item->page1 = (phys & 0xfffff000) + 0x1000;
171 head->next = (unsigned) item;
174 DBG("ept%d %s queue len %x, buffer %x\n",
175 num, in ? "in" : "out", len, phys);
182 flush_cache(phys, len);
183 flush_cache((unsigned long)item, sizeof(struct ept_queue_item));
184 writel(bit, &udc->epprime);
189 static void handle_ep_complete(struct mv_ep *ep)
191 struct ept_queue_item *item;
193 num = ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
194 in = (ep->desc->bEndpointAddress & USB_DIR_IN) != 0;
196 ep->desc = &ep0_out_desc;
197 item = controller.items[2 * num + in];
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);
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);
210 usb_ep_queue(&ep->ep, &ep->req, 0);
211 ep->desc = &ep0_in_desc;
215 #define SETUP(type, request) (((type) << 8) | (request))
217 static void handle_setup(void)
219 struct usb_request *req = &controller.ep[0].req;
220 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
221 struct ept_queue_head *head;
222 struct usb_ctrlrequest r;
224 int num, in, _num, _in, i;
226 head = controller.epts + 2 * 0 + 0;
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);
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);
239 if ((r.wValue == 0) && (r.wLength == 0)) {
241 for (i = 0; i < NUM_ENDPOINTS; i++) {
242 if (!controller.ep[i].desc)
244 num = controller.ep[i].desc->bEndpointAddress
245 & USB_ENDPOINT_NUMBER_MASK;
246 in = (controller.ep[i].desc->bEndpointAddress
248 if ((num == _num) && (in == _in)) {
250 usb_ep_queue(controller.gadget.ep0,
258 case SETUP(USB_RECIP_DEVICE, USB_REQ_SET_ADDRESS):
260 * write address delayed (will take effect
261 * after the next IN txn)
263 writel((r.wValue << 25) | (1 << 24), &udc->devaddr);
265 usb_ep_queue(controller.gadget.ep0, req, 0);
268 case SETUP(USB_DIR_IN | USB_RECIP_DEVICE, USB_REQ_GET_STATUS):
270 buf = (char *)req->buf;
271 buf[0] = 1 << USB_DEVICE_SELF_POWERED;
273 usb_ep_queue(controller.gadget.ep0, req, 0);
276 /* pass request up to the gadget driver */
277 if (controller.driver)
278 status = controller.driver->setup(&controller.gadget, &r);
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]);
289 static void stop_activity(void)
292 struct ept_queue_head *head;
293 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
294 writel(readl(&udc->epcomp), &udc->epcomp);
295 writel(readl(&udc->epstat), &udc->epstat);
296 writel(0xffffffff, &udc->epflush);
298 /* error out any pending reqs */
299 for (i = 0; i < NUM_ENDPOINTS; i++) {
301 writel(0, &udc->epctrl[i]);
302 if (controller.ep[i].desc) {
303 num = controller.ep[i].desc->bEndpointAddress
304 & USB_ENDPOINT_NUMBER_MASK;
305 in = (controller.ep[i].desc->bEndpointAddress
307 head = controller.epts + (num * 2) + (in);
308 head->info = INFO_ACTIVE;
315 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
316 unsigned n = readl(&udc->usbsts);
317 writel(n, &udc->usbsts);
320 n &= (STS_SLI | STS_URI | STS_PCI | STS_UI | STS_UEI);
325 DBG("-- reset --\n");
329 DBG("-- suspend --\n");
332 DBG("-- portchange --\n");
333 bit = (readl(&udc->portsc) >> 26) & 3;
335 controller.gadget.speed = USB_SPEED_HIGH;
336 for (i = 1; i < NUM_ENDPOINTS && n; i++)
337 if (controller.ep[i].desc)
338 controller.ep[i].ep.maxpacket = 512;
340 controller.gadget.speed = USB_SPEED_FULL;
345 printf("<UEI %x>\n", readl(&udc->epcomp));
347 if ((n & STS_UI) || (n & STS_UEI)) {
348 n = readl(&udc->epstat);
352 n = readl(&udc->epcomp);
354 writel(n, &udc->epcomp);
356 for (i = 0; i < NUM_ENDPOINTS && n; i++) {
357 if (controller.ep[i].desc) {
358 num = controller.ep[i].desc->bEndpointAddress
359 & USB_ENDPOINT_NUMBER_MASK;
360 in = (controller.ep[i].desc->bEndpointAddress
362 bit = (in) ? EPT_TX(num) : EPT_RX(num);
364 handle_ep_complete(&controller.ep[i]);
370 int usb_gadget_handle_interrupts(void)
373 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
375 value = readl(&udc->usbsts);
382 static int mv_pullup(struct usb_gadget *gadget, int is_on)
384 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
387 writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RST, &udc->usbcmd);
390 writel((unsigned) controller.epts, &udc->epinitaddr);
392 /* select DEVICE mode */
393 writel(USBMODE_DEVICE, &udc->usbmode);
395 writel(0xffffffff, &udc->epflush);
397 /* Turn on the USB connection by enabling the pullup resistor */
398 writel(USBCMD_ITC(MICRO_8FRAME) | USBCMD_RUN, &udc->usbcmd);
401 writel(USBCMD_FS2, &udc->usbcmd);
403 if (controller.driver)
404 controller.driver->disconnect(gadget);
410 void udc_disconnect(void)
412 struct mv_udc *udc = (struct mv_udc *)controller.ctrl->hcor;
415 writel(USBCMD_FS2, &udc->usbcmd);
417 if (controller.driver)
418 controller.driver->disconnect(&controller.gadget);
421 static int mvudc_probe(void)
423 struct ept_queue_head *head;
426 const int num = 2 * NUM_ENDPOINTS;
428 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);
433 /* The QH list must be aligned to 4096 bytes. */
434 controller.epts = memalign(eplist_align, eplist_sz);
435 if (!controller.epts)
437 memset(controller.epts, 0, eplist_sz);
439 for (i = 0; i < 2 * NUM_ENDPOINTS; i++) {
441 * 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.
447 head = controller.epts + i;
449 head->config = CONFIG_MAX_PKT(EP0_MAX_PACKET_SIZE)
450 | CONFIG_ZLT | CONFIG_IOS;
452 head->config = CONFIG_MAX_PKT(EP_MAX_PACKET_SIZE)
454 head->next = TERMINATE;
457 controller.items[i] = memalign(roundup(32, ARCH_DMA_MINALIGN),
458 sizeof(struct ept_queue_item));
461 INIT_LIST_HEAD(&controller.gadget.ep_list);
464 memcpy(&controller.ep[0].ep, &mv_ep_init[0], sizeof(*mv_ep_init));
465 controller.ep[0].desc = &ep0_in_desc;
466 controller.gadget.ep0 = &controller.ep[0].ep;
467 INIT_LIST_HEAD(&controller.gadget.ep0->ep_list);
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);
480 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
487 if (!driver->bind || !driver->setup || !driver->disconnect)
489 if (driver->speed != USB_SPEED_FULL && driver->speed != USB_SPEED_HIGH)
492 ret = usb_lowlevel_init(0, (void **)&controller.ctrl);
498 udc = (struct mv_udc *)controller.ctrl->hcor;
500 /* select ULPI phy */
501 writel(PTS(PTS_ENABLE) | PFSC, &udc->portsc);
504 ret = driver->bind(&controller.gadget);
506 DBG("driver->bind() returned %d\n", ret);
509 controller.driver = driver;
514 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)